const db = require("../dbs/db");
const { encrypt, comparePassword } = require('../lib/bcrypt')
const jwt = require("jsonwebtoken")

class AdminUser_Controller {
    static async create(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        //接收客服端
        let req = ctx.request.body;
        if (req.name) {
            try {
                const saltPassword = await encrypt(req.pwd)
                console.log(saltPassword)
                const ret = await db.AdminUser.create({
                    name: req.name,
                    birth: req.birth,
                    crtm: req.crtm,
                    email: req.email,
                    mdtm: req.mdtm,
                    sex: req.sex,
                    pwd: saltPassword,
                    phone: req.phone
                });
                const data = await db.AdminUser.findOne({
                    where: { name: ret.name }
                })

                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '创建成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '创建失败',
                    data: err
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 200,
                msg: '参数不齐全'
            }
        }
    }

    static async detail(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        let req = ctx.request.body;
        if (req.name) {
            try {
                // 查询文章详情模型
                let data = await db.AdminUser.findOne({
                    where: { name: req.name }
                })
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '查询成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '查询失败',
                    data
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 416,
                msg: 'ID必须传'
            }
        }
    }

    static async update(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        let req = ctx.request.body;
        let cid = ctx.params.id;
        if (req.name && id) {
            try {
                await db.AdminUser.update({
                    name: req.name,
                    birth: req.birth,
                    crtm: req.crtm,
                    email: req.email,
                    mdtm: req.mdtm,
                    sex: req.sex,
                    pwd: req.pwd,
                    phone: req.phone
                }, { where: { id: cid } })
                let data = await db.AdminUser.findOne({
                    where: { name: req.name }
                })
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '更新成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '更新失败',
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 416,
                msg: '缺少ID值或name值'
            }
        }
    }

    static async delete(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        let cid = ctx.params.id;
        try {
            let data = await db.AdminUser.findOne({
                where: { id: cid }
            })
            await db.AdminUser.destroy({
                where: {
                    id: id
                }
            })
            ctx.response.status = 200;
            ctx.body = {
                code: 200,
                msg: `删除${data.name}成功`,
            }
        } catch (err) {
            ctx.response.status = 412;
            ctx.body = {
                code: 412,
                msg: '删除失败',
            }
        }
    }

    static async list(ctx, next) {
        const token = String(ctx.request.headers.authorization || '').split(' ').pop()
        if (!token) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        const { id } = jwt.verify(token, 'glory-of-kings')
        if (!id) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        ctx.req.user = await db.AdminUser.findOne({
            where: { id: id }
        })
        if (!ctx.req.user) {
            ctx.response.status = 401;
            ctx.body = {
                code: 401,
                msg: '请先登录',
            }
            return
        }
        await next()
        let data = await db.AdminUser.findAll()
        ctx.response.status = 200;
        ctx.body = {
            code: 200,
            msg: '查询成功',
            data
        }
    }

    static async login(ctx) {
        const { pwd, name } = ctx.request.body
        if (pwd && name) {
            try {
                const user = await db.AdminUser.findOne({
                    where: { name: name }
                })
                if (!user) {
                    ctx.response.status = 422;
                    ctx.body = {
                        code: 412,
                        msg: '用户不存在',
                    }
                } else {
                    const isMatch = await comparePassword(pwd, user.pwd)
                    if (!isMatch) {
                        ctx.response.status = 422;
                        ctx.body = {
                            code: 412,
                            msg: '密码不正确',
                        }
                    } else {
                        const token = jwt.sign({ id: user.id }, 'glory-of-kings', { expiresIn: '1h' })
                        ctx.response.status = 200;
                        ctx.body = {
                            code: 412,
                            token
                        }
                    }
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '登录失败',
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 416,
                msg: '请输入用户密码'
            }
        }
    }
}

module.exports = AdminUser_Controller;